package br.com.felix.fwt.ui;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import br.com.felix.fwt.ui.exception.ComponentRenderException;

public class RadioGroup extends BaseComponent {

	private static final long serialVersionUID = -4341918712371586046L;

	private List<RadioButton> lstRadioButtons = new ArrayList<RadioButton>();

	public RadioGroup() {
	}



	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		String name = getName();
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}



	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		RadioGroup other = (RadioGroup) obj;
		String name = getName();
		String otherName = other.getName();
		if (name == null) {
            if (otherName != null)
				return false;
		}
		else
			if (!name.equals(otherName))
				return false;
		return true;
	}



	@Override
	public void write(PrintWriter out) throws ComponentRenderException {
		// Button group has no interface, but must be present in the component
		// tree.
	}
	
	
	
	public void addRadioButton(RadioButton ...buttons){
		for(RadioButton rdo:buttons){
			lstRadioButtons.add(rdo);
			rdo.setRadioGroup(this);
		}
	}



	@Override
	public void setValue(String newValue) {
		super.setValue(newValue);
		for(RadioButton rdo:lstRadioButtons){
			if(newValue.equals(rdo.getValue())){
				rdo.setChecked(true);
			}
			else{
				rdo.setChecked(false);
			}
		}
	}



    @Override
    public boolean isInputControl() {
        return true;
    }
	

}
